package cn.group.service.impl;

import cn.group.mapper.CommentMapper;
import cn.group.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.entity.Comment;

import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public PageInfo findComments(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize); //会对下面第一个查询结果进行分页
        List<Comment> list = commentMapper.selectAllComments();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public PageInfo findCommentsWithLike(Map<String, String> paramsMap) {
        Object current = paramsMap.get("currentPage");
        Object size = paramsMap.get("pageSize");
        int currentPage = Integer.parseInt(String.valueOf(current));
        int pageSize = Integer.parseInt(String.valueOf(size));

        for (String key : paramsMap.keySet()) {
            String value = paramsMap.get(key);
            if ("".equals(value)) {
                paramsMap.put(key, null);
            }
        }
        PageHelper.startPage(currentPage, pageSize); //会对下面第一个查询结果进行分页
        List<Comment> listLike = commentMapper.selectAllCommentsWithLike(paramsMap);
        PageInfo pageInfo = new PageInfo(listLike);
        return pageInfo;
    }

    @Override
    public PageInfo findCommentsWithDate(Map<String, String> paramsMap) {
        Object current = paramsMap.get("currentPage");
        Object size = paramsMap.get("pageSize");
        int currentPage = Integer.parseInt(String.valueOf(current));
        int pageSize = Integer.parseInt(String.valueOf(size));

        for (String key : paramsMap.keySet()) {
            String value = paramsMap.get(key);
            if ("".equals(value)) {
                paramsMap.put(key, null);
            }
        }
        PageHelper.startPage(currentPage, pageSize); //会对下面第一个查询结果进行分页
        List<Comment> listLike = commentMapper.selectAllWithDate(paramsMap);
        PageInfo pageInfo = new PageInfo(listLike);
        return pageInfo;
    }

    @Override
    public Integer deleteCommentById(Long commentId) {
        return commentMapper.deleteById(commentId);
    }
}
